layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    table.render({
        elem: '#LAY-apppatch-manage'
        , url: '/admin/apppatch/getlist'
        , cols: [[
            {field: 'id', width: 70, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'base_code', title: '需要补丁版本编号', width: 200}
            , {field: 'patch_id', title: '补丁名称', width: 200}
            , {field: 'patch_url', title: '下载地址'}
            , {
                field: '', title: '后台管理员', width: 200, templet: function (d) {
                    return d.admin ? d.admin.name : '';
                }
            }
            , {field: 'create_time', title: '添加时间'}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-apppatch-opera'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    form.on('submit(apppatch-add)', function (obj) {
        var data = obj.field;
        //提交
        admin.req({
            url: '/admin/apppatch/add_post'
            , data: data
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                    }, 1200);
                }
            }
        });
        return false;
    });

    form.on('submit(apppatch-edit)', function (obj) {
        var data = obj.field;
        //提交
        admin.req({
            url: '/admin/apppatch/edit_post'
            , data: data
            , success: function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg, {icon: 1});
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                    }, 1200);
                }
            }
        });
        return false;
    });

    //监听工具条
    table.on('tool(LAY-apppatch-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确定要删除该信息吗？', function (index) {

                admin.req({
                    url: '/admin/apppatch/del_post'
                    , data: {"id": obj.data.id}
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-apppatch-manage");
                            layer.msg(res.msg, {icon: 1});
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: '编辑'
                , content: '/admin/apppatch/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['650px', '500px']
            });
        }
    });


    exports('app_patch', {})
});

